package com.ce.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ce.server.common.RespBean;
import com.ce.server.common.RespPageBean;
import com.ce.server.pojo.Admin;
import com.ce.server.pojo.Operlog;
import com.ce.server.pojo.Working;
import com.ce.server.service.IOperlogService;
import com.ce.server.service.IWorkingService;
import com.ce.server.util.AdminUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.security.Principal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-26
 */
@RestController
@RequestMapping("/emplyee/schedule/")
public class WorkingController {

  private static final Logger log = LoggerFactory.getLogger(WorkingController.class);
  @Autowired
  private IWorkingService workingService;
  @Autowired
  private IOperlogService operlogService;


  @ApiOperation(value = "分页查询所有记录")
  @GetMapping("/")
  public RespPageBean getAllRecords(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "10") Integer size,
                                    Date date, String last_next) {
    return workingService.getAllRecords(currentPage, size, date, last_next);
  }

  @ApiOperation(value = "添加值班记录")
  @PostMapping("/")
  public RespBean addRecord(@RequestBody Working working, Principal principal) {
    Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
    operlogService.save(new Operlog().setWno(currentAdmin.getWno())
            .setWname(currentAdmin.getWname())
            .setOper("添加值班记录").setOpertime(LocalDateTime.now()));
    return workingService.save(working) ? RespBean.success("添加成功！") : RespBean.error("添加失败！");
  }

  @ApiOperation(value = "修改了值班记录")
  @PutMapping("/")
  public RespBean updateRecord(@RequestBody Working working) {
    Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
    operlogService.save(new Operlog().setWno(currentAdmin.getWno())
            .setWname(currentAdmin.getWname())
            .setOper("修改了值班记录").setOpertime(LocalDateTime.now()));
    return workingService.updateById(working) ? RespBean.success("更新成功！") : RespBean.error("更新失败！");
  }

  @ApiOperation(value = "删除值班记录")
  @DeleteMapping("/{id}")
  public RespBean deleteRecord(@PathVariable Integer id) {
    Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
    operlogService.save(new Operlog().setWno(currentAdmin.getWno())
            .setWname(currentAdmin.getWname())
            .setOper("删除了值班记录").setOpertime(LocalDateTime.now()));
    return workingService.removeById(id) ? RespBean.success("删除成功！") : RespBean.error("删除失败！");
  }

  @ApiOperation(value = "查询单条值班记录")
  @GetMapping("/wno/{wno}")
  public Working getOneRecord(@PathVariable String wno) {
    List<Working> list = workingService.list(new LambdaQueryWrapper<Working>().eq(Working::getWno, wno));
    if (list != null)
      return list.get(0);
    return null;
  }

  @InitBinder
  public void initBinder(WebDataBinder binder, WebRequest request) {

    //转换日期
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
  }


}
